package up_down_share.component;

import up_down_share.updaonwshare.v01.R;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

// ----------------------------------------------------------------------------------------------------
// ----- Web Service -----
// ----------------------------------------------------------------------------------------------------
public class WebService {
	// --------------------------------------------------
	// ----- constants -----
	// --------------------------------------------------
	public static final String BASE_URL = "http://www.farmplus.com.hk/products/fpspty01/webservice.php";

	// --------------------------------------------------
	// ----- properties -----
	// --------------------------------------------------
	private final String account = "";
	private final String password = "";
	private int responseCode = 0;

	// --------------------------------------------------
	// ----- extends -----
	// --------------------------------------------------
	public WebService() {

	}

	// --------------------------------------------------
	// ----- functions -----
	// --------------------------------------------------
	// body输入参数是这样一种格�?	// name = something&age=int

	private String get(final String body) {
		// full URL
		// 添加参数�?		// 传给服务器的是一串URL地址
		// 服务器，返回的是�?��用JSON封装的数据，JSON是一组字符串
		final String requestURL = String.format("%s?%s", WebService.BASE_URL,
				body);
		String result = null;

		final HttpClient httpclient = new DefaultHttpClient();
		final HttpGet httpget = new HttpGet(requestURL);
		try {
			final HttpResponse httpresponse = httpclient.execute(httpget);

			// 处理返回的数�?			
			final HttpEntity entity = httpresponse.getEntity();
			responseCode = httpresponse.getStatusLine().getStatusCode();

			if (entity != null) {
				result = EntityUtils.toString(entity, "UTF-8");
			}

		} catch (final ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (final IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result;

	}

	// 输入参数的形�?key=value&key=value
	private String post(final String body) {
	

		final HttpClient httpclient = new DefaultHttpClient();
		final HttpPost httppost = new HttpPost(WebService.BASE_URL);
		String result = null;

		// 要添加一个解析body的方�?		
		ArrayList<String>[] lists = new ArrayList[2];
		lists = parserKeyValue(parser(body));

		final List<NameValuePair> params = new ArrayList<NameValuePair>();
		for (int i = 0; i < lists[0].size(); i++) {
			params.add(new BasicNameValuePair(lists[0].get(i), lists[1].get(i)));
		}

		try {
			httppost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

			final HttpResponse httpresponse = httpclient.execute(httppost);
			final HttpEntity httpentity = httpresponse.getEntity();
			responseCode = httpresponse.getStatusLine().getStatusCode();

			if (httpentity != null) {
				result = EntityUtils.toString(httpentity, "UTF-8");
			}

		} catch (final UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (final ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (final IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result;

	}

	public String getParty(final String partyID) {
		// �?��添加账号和密�?		// 然后构建成一个body
		// 作为输入参数给post
		final String body = "&partyID=" + partyID;

		return post(body);
	}

	// 返回包含的元素是key=value的集�?	// 输入参数的形�?key=value&key=value
	private ArrayList<String> parser(final String raw) {

		final ArrayList<String> formatResult = new ArrayList<String>();
		final Pattern p = Pattern.compile("\\w+=\\w+");
		final Matcher m = p.matcher(raw);

		while (m.find()) {

			if (m.start() != m.end()) {

				formatResult.add(raw.substring(m.start(), m.end()));
			}
		}

		return formatResult;
	}

	// 返回的是两个list集合:key集合，value集合
	// 输入参数的形�? key=value
	private ArrayList<String>[] parserKeyValue(final ArrayList<String> raw) {

		final ArrayList<String> key = new ArrayList<String>();
		final ArrayList<String> value = new ArrayList<String>();
		final ArrayList<String>[] lists = new ArrayList[2];

		final Pattern inner = Pattern.compile("\\w+");
		for (int i = 0; i < raw.size(); i++) {
			final Matcher inner_m = inner.matcher(raw.get(i));

			int j = 1;
			while (inner_m.find()) {

				if (inner_m.start() != inner_m.end()) {

					if (j % 2 != 0) {
						key.add(raw.get(i).substring(inner_m.start(),
								inner_m.end()));
						j++;
					} else {
						value.add(raw.get(i).substring(inner_m.start(),
								inner_m.end()));
						j++;
					}

				}

			}
		}

		lists[0] = key;
		lists[1] = value;

		return lists;
	}

}
